package com.learn.xueer.ui.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.learn.xueer.R
import com.learn.xueer.data.model.bean.PkResponse
import com.learn.xueer.data.model.bean.QuestionBean
import java.util.Random

class AnswerAdapter(
    val questions: ArrayList<QuestionBean>,
    val pkInfo: PkResponse,
    val callback: (Int, Int, String) -> Unit
) :
    PagerAdapter() {

    private val random = Random()

    override fun getCount(): Int {
        return questions.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view =
            LayoutInflater.from(container.context)
                .inflate(R.layout.pk_item_answer, container, false)
        container.addView(view)
        view.findViewById<TextView>(R.id.tv_word).text = pkInfo.wordsList[position].words
        val questionBean = questions[position]
        questionBean.wordsList.forEachIndexed { select, word ->
            val ll = view.findViewById<LinearLayout>(R.id.ll_question)
            val item = LayoutInflater.from(container.context)
                .inflate(R.layout.pk_item_question, ll, false)
            ll.addView(item)
            item.findViewById<TextView>(R.id.tv_word_text).text = word.pretations
            item.setOnClickListener {
                if (questionBean.hasAnswer == -1) {
                    var index = 0
                    for (i in 0 until questionBean.wordsList.size) {
                        if (questionBean.wordsList[i].wId == pkInfo.wordsList[position].wId) {
                            index = i
                            break
                        }
                    }
                    questionBean.answerIndex = random.nextInt(questionBean.wordsList.size)

                    for (i in 0 until questionBean.wordsList.size) {
                        if (i == index) {
                            if(select ==index){
                                questionBean.hasAnswer = 1
                            }else{
                                questionBean.hasAnswer = 0
                            }
                            ll.getChildAt(i).findViewById<TextView>(R.id.tv_word_text)
                                .setTextColor(Color.parseColor("#0CB76C"))
                        } else if (i == select) {
                            ll.getChildAt(i).findViewById<TextView>(R.id.tv_word_text)
                                .setTextColor(Color.parseColor("#F45555"))
                            ll.getChildAt(i).findViewById<ImageView>(R.id.iv_left_err).visibility =
                                View.VISIBLE
                        } else {
                            ll.getChildAt(i).alpha = 0.3f
                        }
                        if (i == questionBean.answerIndex && i != index) {
                            ll.getChildAt(i).findViewById<TextView>(R.id.tv_word_text)
                                .setTextColor(Color.parseColor("#F45555"))
                            ll.getChildAt(i).findViewById<ImageView>(R.id.iv_right_err).visibility =
                                View.VISIBLE
                            ll.getChildAt(i).alpha = 1f
                        }
                    }

                    if (index == questionBean.answerIndex) {
                        questionBean.otherAnswer = 1
                    } else {
                        questionBean.otherAnswer = 0
                    }
                    callback.invoke(
                        position,
                        pkInfo.wordsList[position].id,
                        questionBean.wordsList[select].wId
                    )
                }
            }
        }
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }
}